﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ATW.MT.CommonClass;
using System.Data;
using ATW.MT.BUS;

namespace ATW.MT.SellManage
{
    public partial class SellerManage : CommonPage
    {
        bus_T_appuser au = new bus_T_appuser();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        private void BindData()
        {
            string name = txtName.Value.Trim();
            string status = ddlStatus.SelectedValue;
            try
            {
                if (!common.filterChar(name))
                {
                    Tips("查询条件中含有危险字符！");
                    return;
                }
                DataTable dt = au.SellerPagerQuery(name, status, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize);
                if (dt != null && dt.Rows.Count > 0)
                {
                    AspNetPager1.RecordCount = Convert.ToInt32(dt.Rows[0]["totalpagesize"]);
                }
                else
                {
                    AspNetPager1.RecordCount = 0;
                }
                rpList.DataSource = dt;
                rpList.DataBind();
            }
            catch (Exception ex)
            {

            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            BindData();
        }
        protected void lbnAddSeller_Click(object sender, EventArgs e)
        {//新增销售
            string url = "../SellManage/_addSeller.aspx?r=" + new Random().Next(1000).ToString();
            OpenDialog("新增销售", url, "height=300,width=400");
        }
        protected void lbnDel_Click(object sender, EventArgs e)
        {//删除
            LinkButton lbn = (LinkButton)sender;
            string uid = lbn.CommandArgument;
            try
            {
                if (au.SellerDel(uid))
                {
                    Tips("删除成功！");
                    BindData();
                }
                else
                {
                    Tips("删除失败！");
                }
            }
            catch (Exception ex)
            {

            }
        }
        protected void lbnRecover_Click(object sender, EventArgs e)
        {//恢复
            LinkButton lbn = (LinkButton)sender;
            string uid = lbn.CommandArgument;
            try
            {
                int result = au.SellRecover(uid);
                if (result > 0)
                {
                    Tips("恢复成功！");
                    BindData();
                }
                else
                {
                    Tips("恢复失败！");
                }
            }
            catch (Exception ex)
            {

            }
        }
        protected void lbnAllocate_Click(object sender, EventArgs e)
        {//分配
            LinkButton lbn = (LinkButton)sender;
            try
            {
                DataTable dt = au.GetUnlockedCustomer();
                if (dt != null && dt.Rows.Count == 0)
                {
                    Tips("没有可以分配的用户");
                    return;
                }
            }
            catch (Exception ex)
            {
 
            }
            string code = lbn.CommandArgument;
            string url = "../SellManage/_customerList.aspx?code=" + code + "&r=" + new Random().Next(1000).ToString();
            OpenDialog("分配客户", url, "height=400,width=600");
        }
        protected void AspNetPager1_pageChanged(object sender, EventArgs e)
        {
            BindData();
        }
        private void Tips(string s)
        {//提示
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                var divTips=new jSharp.popUpDiv('divMsg');
                document.getElementById('msgContent').innerHTML='" + s + @"';
                divTips.Show();
                var t=setTimeout(function(){
                    divTips.Close();
                    clearTimeout(t);
                },1000);
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "tips", strScript.ToString());
        }
    }
}